/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package bank_access;

import java.io.IOException;
import java.net.UnknownHostException;
import mware_lib.Communicator.Client;
import mware_lib.MiddlewareException;
import mware_lib.StringDecoder;

/**
 * konkrete implementierung der abstrakten ManagerImplBase klasse
 * @author Gortex
 */
public class ManagerProxy extends ManagerImplBase {

    //IP und Port des originalobjekts
    private String ipAdresse;
    private int port;
    private String reference;
    private Client client;

    protected ManagerProxy(String ip, int port, String reference) {
        this.ipAdresse = ip;
        this.port = port;
        this.reference = reference;
        
//        System.out.println("ManagerProxy gestartet mit ipAdresse=" + ipAdresse + " port" + port + " reference" + reference );
    }

    /**
     * Proxymethode die mthode zu einem String macht und diesen String
     * an das skeleton auf der anderen Seite übergibt
     * @param owner
     * @param branch
     * @return 
     */
    @Override
    public String createAccount(String owner, String branch) {
        try {
            String message = "methodname:createAccount;param1typ:String;param1value:" + owner + ";param2typ:String;param2value:" + branch;

            client = new Client(ipAdresse, port);
            client.send(reference + ";" + message);
            String reply = client.receive();

            if (!StringDecoder.hasException(reply)) {
                if (!StringDecoder.getReturnType(reply).equals("String")) {
                    client.close();
                    throw new MiddlewareException("ManagerProxy: Return Type missmatch.");
                } else {
                    client.close();
                    return StringDecoder.getReturnValue(reply);
                }
            } else {
                client.close();
                throw new MiddlewareException("ManagerProxy: " + StringDecoder.getReturnValue(reply));
            }

        } catch (UnknownHostException ex) {
            throw new MiddlewareException("ManagerProxy: Skeleton nicht erreichbar Message: " + ex.getMessage());
        } catch (IOException ex) {
            throw new MiddlewareException("ManagerProxy: Skeleton nicht erreichbar Message: " + ex.getMessage());
        }
    }

   
}
